"""
DAQ217设备Python绑定
封装DAQ217设备的C接口函数
"""

import ctypes
from typing import List, Optional, Tuple
from .base_device_binding import BaseDeviceBinding


class DAQ217Binding(BaseDeviceBinding):
    """DAQ217设备Python绑定类"""
    
    def __init__(self, dll_path: str):
        """
        初始化DAQ217设备绑定
        
        Args:
            dll_path: DLL文件路径
        """
        super().__init__(dll_path)
        self._setup_daq217_function_signatures()
    
    def _setup_daq217_function_signatures(self):
        """设置DAQ217特有的函数签名"""
        # 设备生命周期管理
        self.dll.DAQ217_New.argtypes = []
        self.dll.DAQ217_New.restype = ctypes.POINTER(ctypes.c_uint32)
        
        self.dll.DAQ217_Delete.argtypes = [ctypes.POINTER(ctypes.c_uint32)]
        self.dll.DAQ217_Delete.restype = None
        
        # 设备连接和初始化
        self.dll.DAQ217_UseBackend.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.POINTER(ctypes.c_uint32)
        ]
        self.dll.DAQ217_UseBackend.restype = ctypes.c_bool
        
        self.dll.DAQ217_InitializeDevice.argtypes = [ctypes.POINTER(ctypes.c_uint32)]
        self.dll.DAQ217_InitializeDevice.restype = ctypes.c_bool
        
        self.dll.DAQ217_ConnectedDevice.argtypes = [ctypes.POINTER(ctypes.c_uint32)]
        self.dll.DAQ217_ConnectedDevice.restype = ctypes.c_bool
        
        self.dll.DAQ217_DisconnectedDevice.argtypes = [ctypes.POINTER(ctypes.c_uint32)]
        self.dll.DAQ217_DisconnectedDevice.restype = ctypes.c_bool
        
        # ADC配置函数
        self.dll.DAQ217_ConfigADCTrigger.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int
        ]
        self.dll.DAQ217_ConfigADCTrigger.restype = ctypes.c_bool
        
        self.dll.DAQ217_ConfigADCSampleRate.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_int
        ]
        self.dll.DAQ217_ConfigADCSampleRate.restype = ctypes.c_bool
        
        self.dll.DAQ217_ConfigADCVoltage.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_int
        ]
        self.dll.DAQ217_ConfigADCVoltage.restype = ctypes.c_bool
        
        self.dll.DAQ217_ConfigADCVoltage_Batch.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.POINTER(ctypes.c_int), 
            ctypes.c_int
        ]
        self.dll.DAQ217_ConfigADCVoltage_Batch.restype = ctypes.c_bool
        
        self.dll.DAQ217_ConfigADCTriggerLength.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_uint32
        ]
        self.dll.DAQ217_ConfigADCTriggerLength.restype = ctypes.c_bool
        
        self.dll.DAQ217_ConfigTriggerTimeout.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_uint32
        ]
        self.dll.DAQ217_ConfigTriggerTimeout.restype = ctypes.c_bool
        
        self.dll.DAQ217_ConfigADCChannel.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_uint8
        ]
        self.dll.DAQ217_ConfigADCChannel.restype = ctypes.c_bool
        
        self.dll.DAQ217_ConfigADCChannel_Batch.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.POINTER(ctypes.c_bool), 
            ctypes.c_int
        ]
        self.dll.DAQ217_ConfigADCChannel_Batch.restype = ctypes.c_bool
        
        self.dll.DAQ217_ConfigACDC.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_uint8
        ]
        self.dll.DAQ217_ConfigACDC.restype = ctypes.c_bool
        
        self.dll.DAQ217_ConfigADCTriggerSource.argtypes = [ctypes.POINTER(ctypes.c_uint32)]
        self.dll.DAQ217_ConfigADCTriggerSource.restype = ctypes.c_bool
        
        # 数据采集控制
        self.dll.DAQ217_StartCollection.argtypes = [ctypes.POINTER(ctypes.c_uint32)]
        self.dll.DAQ217_StartCollection.restype = ctypes.c_bool
        
        self.dll.DAQ217_StopCollection.argtypes = [ctypes.POINTER(ctypes.c_uint32)]
        self.dll.DAQ217_StopCollection.restype = ctypes.c_bool
        
        # 数据读取函数
        self.dll.DAQ217_TryReadData.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_uint8, 
            ctypes.POINTER(ctypes.c_float), 
            ctypes.c_uint32, 
            ctypes.c_uint32
        ]
        self.dll.DAQ217_TryReadData.restype = ctypes.c_bool
        
        self.dll.DAQ217_TryReadData_Point.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_uint8, 
            ctypes.POINTER(ctypes.c_float)
        ]
        self.dll.DAQ217_TryReadData_Point.restype = ctypes.c_bool
        
        self.dll.DAQ217_TryReadData_Batch.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_uint8, 
            ctypes.POINTER(ctypes.c_float), 
            ctypes.c_uint32
        ]
        self.dll.DAQ217_TryReadData_Batch.restype = ctypes.c_bool
        
        # 缓冲区管理
        self.dll.DAQ217_ClearDataBuffer.argtypes = [ctypes.POINTER(ctypes.c_uint32)]
        self.dll.DAQ217_ClearDataBuffer.restype = ctypes.c_bool
        
        self.dll.DAQ217_ClearDataBuffer_Channel.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_uint8
        ]
        self.dll.DAQ217_ClearDataBuffer_Channel.restype = ctypes.c_bool
        
        # 设备信息获取
        self.dll.DAQ217_ProductTestGetSN.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_char_p, 
            ctypes.c_int
        ]
        self.dll.DAQ217_ProductTestGetSN.restype = ctypes.c_bool
        
        self.dll.DAQ217_GetSN.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_char_p, 
            ctypes.c_int
        ]
        self.dll.DAQ217_GetSN.restype = ctypes.c_bool
        
        self.dll.DAQ217_SystemOpGetDeviceName.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_char_p, 
            ctypes.c_int
        ]
        self.dll.DAQ217_SystemOpGetDeviceName.restype = ctypes.c_bool
        
        self.dll.DAQ217_GetDeviceName.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_char_p, 
            ctypes.c_int
        ]
        self.dll.DAQ217_GetDeviceName.restype = ctypes.c_bool
        
        # 版本信息获取
        self.dll.DAQ217_BasicInfoGetDeviceType.argtypes = [ctypes.POINTER(ctypes.c_uint32)]
        self.dll.DAQ217_BasicInfoGetDeviceType.restype = ctypes.c_bool
        
        self.dll.DAQ217_BasicInfoGetHardwareVersion.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_char_p, 
            ctypes.c_int
        ]
        self.dll.DAQ217_BasicInfoGetHardwareVersion.restype = ctypes.c_bool
        
        self.dll.DAQ217_BasicInfoGetBootLoaderVersion.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_char_p, 
            ctypes.c_int
        ]
        self.dll.DAQ217_BasicInfoGetBootLoaderVersion.restype = ctypes.c_bool
        
        self.dll.DAQ217_BasicInfoGetSoftwareVersion.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_char_p, 
            ctypes.c_int
        ]
        self.dll.DAQ217_BasicInfoGetSoftwareVersion.restype = ctypes.c_bool
        
        self.dll.DAQ217_GetDeviceType.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_char_p, 
            ctypes.c_int
        ]
        self.dll.DAQ217_GetDeviceType.restype = ctypes.c_bool
        
        self.dll.DAQ217_GetHardwareVersion.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_char_p, 
            ctypes.c_int
        ]
        self.dll.DAQ217_GetHardwareVersion.restype = ctypes.c_bool
        
        self.dll.DAQ217_GetBootLoaderVersion.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_char_p, 
            ctypes.c_int
        ]
        self.dll.DAQ217_GetBootLoaderVersion.restype = ctypes.c_bool
        
        self.dll.DAQ217_GetSoftwareVersion.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_char_p, 
            ctypes.c_int
        ]
        self.dll.DAQ217_GetSoftwareVersion.restype = ctypes.c_bool
        
        # ADC参数获取
        self.dll.DAQ217_GetADCLegalSampleRate.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.POINTER(ctypes.c_int)
        ]
        self.dll.DAQ217_GetADCLegalSampleRate.restype = ctypes.c_bool
        
        self.dll.DAQ217_GetCurrentSampleRate.argtypes = [ctypes.POINTER(ctypes.c_uint32)]
        self.dll.DAQ217_GetCurrentSampleRate.restype = ctypes.c_uint32
        
        self.dll.DAQ217_GetADCLegalChannelSize.argtypes = [ctypes.POINTER(ctypes.c_uint32)]
        self.dll.DAQ217_GetADCLegalChannelSize.restype = ctypes.c_uint8
        
        self.dll.DAQ217_GetADCBufferDataSize.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_int
        ]
        self.dll.DAQ217_GetADCBufferDataSize.restype = ctypes.c_uint32
        
        self.dll.DAQ217_GetADCBufferDataMaxSize.argtypes = [ctypes.POINTER(ctypes.c_uint32)]
        self.dll.DAQ217_GetADCBufferDataMaxSize.restype = ctypes.c_uint32
        
        self.dll.DAQ217_GetADCLegalLengthMin.argtypes = [ctypes.POINTER(ctypes.c_uint32)]
        self.dll.DAQ217_GetADCLegalLengthMin.restype = ctypes.c_uint32
        
        self.dll.DAQ217_GetADCLegalLengthMax.argtypes = [ctypes.POINTER(ctypes.c_uint32)]
        self.dll.DAQ217_GetADCLegalLengthMax.restype = ctypes.c_uint32
        
        self.dll.DAQ217_GetChannelEnableState.argtypes = [ctypes.POINTER(ctypes.c_uint32)]
        self.dll.DAQ217_GetChannelEnableState.restype = ctypes.c_uint8
        
        # 参数验证
        self.dll.DAQ217_ADCSampleRateIsLegal.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_uint32
        ]
        self.dll.DAQ217_ADCSampleRateIsLegal.restype = ctypes.c_bool
        
        self.dll.DAQ217_ADCRangeIsLegal.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.c_int
        ]
        self.dll.DAQ217_ADCRangeIsLegal.restype = ctypes.c_bool
        
        # 配置信息获取
        self.dll.DAQ217_GetADCLegalChannel.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.POINTER(ctypes.c_int)
        ]
        self.dll.DAQ217_GetADCLegalChannel.restype = ctypes.c_bool
        
        self.dll.DAQ217_GetADCLegalVoltage.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.POINTER(ctypes.c_int)
        ]
        self.dll.DAQ217_GetADCLegalVoltage.restype = ctypes.c_bool
        
        self.dll.DAQ217_GetCurrentTriggerType.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.POINTER(ctypes.c_uint32)
        ]
        self.dll.DAQ217_GetCurrentTriggerType.restype = ctypes.c_bool
        
        self.dll.DAQ217_GetCurrentTriggerMode.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.POINTER(ctypes.c_uint32)
        ]
        self.dll.DAQ217_GetCurrentTriggerMode.restype = ctypes.c_bool
        
        self.dll.DAQ217_GetChannelEnableList.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.POINTER(ctypes.c_bool), 
            ctypes.POINTER(ctypes.c_int)
        ]
        self.dll.DAQ217_GetChannelEnableList.restype = ctypes.c_bool
        
        self.dll.DAQ217_GetADCCurrentVoltage.argtypes = [
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.POINTER(ctypes.c_uint32), 
            ctypes.POINTER(ctypes.c_int)
        ]
        self.dll.DAQ217_GetADCCurrentVoltage.restype = ctypes.c_bool
    
    def create_device(self) -> Optional[ctypes.POINTER(ctypes.c_uint32)]:
        """
        创建设备实例
        
        Returns:
            成功返回设备句柄，失败返回None
        """
        try:
            handle = self.dll.DAQ217_New()
            return handle if handle else None
        except Exception:
            return None
    
    def delete_device(self, handle: ctypes.POINTER(ctypes.c_uint32)) -> None:
        """
        删除设备实例并释放相关资源
        
        Args:
            handle: 设备句柄
        """
        if handle:
            try:
                self.dll.DAQ217_Delete(handle)
            except Exception:
                pass
    
    def use_backend(self, device_handle: ctypes.POINTER(ctypes.c_uint32), 
                    backend_handle: ctypes.POINTER(ctypes.c_uint32)) -> bool:
        """
        设置设备使用的后端
        
        Args:
            device_handle: 设备句柄
            backend_handle: 后端句柄
            
        Returns:
            是否成功
        """
        try:
            return self.dll.DAQ217_UseBackend(device_handle, backend_handle)
        except Exception:
            return False
    
    def initialize_device(self, handle: ctypes.POINTER(ctypes.c_uint32)) -> bool:
        """
        初始化设备
        
        Args:
            handle: 设备句柄
            
        Returns:
            是否成功
        """
        try:
            return self.dll.DAQ217_InitializeDevice(handle)
        except Exception:
            return False
    
    def connect_device(self, handle: ctypes.POINTER(ctypes.c_uint32)) -> bool:
        """
        连接设备
        
        Args:
            handle: 设备句柄
            
        Returns:
            是否成功
        """
        try:
            return self.dll.DAQ217_ConnectedDevice(handle)
        except Exception:
            return False
    
    def disconnect_device(self, handle: ctypes.POINTER(ctypes.c_uint32)) -> bool:
        """
        断开设备连接
        
        Args:
            handle: 设备句柄
            
        Returns:
            是否成功
        """
        try:
            return self.dll.DAQ217_DisconnectedDevice(handle)
        except Exception:
            return False
    
    def config_adc_trigger(self, handle: ctypes.POINTER(ctypes.c_uint32), 
                          trigger_type: int, trigger_mode: int, 
                          channel: int, level: int) -> bool:
        """
        配置ADC触发参数
        
        Args:
            handle: 设备句柄
            trigger_type: 触发类型
            trigger_mode: 触发模式
            channel: 触发通道
            level: 触发电平（毫伏）
            
        Returns:
            是否成功
        """
        try:
            return self.dll.DAQ217_ConfigADCTrigger(handle, trigger_type, trigger_mode, channel, level)
        except Exception:
            return False
    
    def config_adc_sample_rate(self, handle: ctypes.POINTER(ctypes.c_uint32), 
                              sample_rate: int) -> bool:
        """
        配置ADC采样率
        
        Args:
            handle: 设备句柄
            sample_rate: 采样率
            
        Returns:
            是否成功
        """
        try:
            return self.dll.DAQ217_ConfigADCSampleRate(handle, sample_rate)
        except Exception:
            return False
    
    def config_adc_voltage(self, handle: ctypes.POINTER(ctypes.c_uint32), 
                          voltage: int) -> bool:
        """
        配置ADC电压范围
        
        Args:
            handle: 设备句柄
            voltage: 电压范围
            
        Returns:
            是否成功
        """
        try:
            return self.dll.DAQ217_ConfigADCVoltage(handle, voltage)
        except Exception:
            return False
    
    def config_adc_voltage_batch(self, handle: ctypes.POINTER(ctypes.c_uint32), 
                               range_array: List[int]) -> bool:
        """
        批量配置ADC电压范围
        
        Args:
            handle: 设备句柄
            range_array: 电压范围数组
            
        Returns:
            是否成功
        """
        try:
            array_size = len(range_array)
            c_array = self._create_int_array(range_array)
            return self.dll.DAQ217_ConfigADCVoltage_Batch(handle, c_array, array_size)
        except Exception:
            return False
    
    def config_adc_trigger_length(self, handle: ctypes.POINTER(ctypes.c_uint32), 
                                 data_length: int) -> bool:
        """
        配置触发采集的数据长度
        
        Args:
            handle: 设备句柄
            data_length: 数据长度
            
        Returns:
            是否成功
        """
        try:
            return self.dll.DAQ217_ConfigADCTriggerLength(handle, data_length)
        except Exception:
            return False
    
    def config_trigger_timeout(self, handle: ctypes.POINTER(ctypes.c_uint32), 
                              timeout: int) -> bool:
        """
        配置触发超时时间
        
        Args:
            handle: 设备句柄
            timeout: 超时时间（毫秒）
            
        Returns:
            是否成功
        """
        try:
            return self.dll.DAQ217_ConfigTriggerTimeout(handle, timeout)
        except Exception:
            return False
    
    def config_adc_channel(self, handle: ctypes.POINTER(ctypes.c_uint32), 
                          channel: int) -> bool:
        """
        配置ADC通道
        
        Args:
            handle: 设备句柄
            channel: 通道掩码
            
        Returns:
            是否成功
        """
        try:
            return self.dll.DAQ217_ConfigADCChannel(handle, channel)
        except Exception:
            return False
    
    def config_adc_channel_batch(self, handle: ctypes.POINTER(ctypes.c_uint32), 
                               enable_array: List[bool]) -> bool:
        """
        批量配置ADC通道
        
        Args:
            handle: 设备句柄
            enable_array: 通道使能数组
            
        Returns:
            是否成功
        """
        try:
            array_size = len(enable_array)
            c_array = self._create_bool_array(enable_array)
            return self.dll.DAQ217_ConfigADCChannel_Batch(handle, c_array, array_size)
        except Exception:
            return False
    
    def config_acdc(self, handle: ctypes.POINTER(ctypes.c_uint32), 
                   channel_mask: int) -> bool:
        """
        配置AC/DC耦合
        
        Args:
            handle: 设备句柄
            channel_mask: 通道掩码
            
        Returns:
            是否成功
        """
        try:
            return self.dll.DAQ217_ConfigACDC(handle, channel_mask)
        except Exception:
            return False
    
    def config_adc_trigger_source(self, handle: ctypes.POINTER(ctypes.c_uint32)) -> bool:
        """
        配置ADC触发源
        
        Args:
            handle: 设备句柄
            
        Returns:
            是否成功
        """
        try:
            return self.dll.DAQ217_ConfigADCTriggerSource(handle)
        except Exception:
            return False
    
    def start_collection(self, handle: ctypes.POINTER(ctypes.c_uint32)) -> bool:
        """
        开始数据采集
        
        Args:
            handle: 设备句柄
            
        Returns:
            是否成功
        """
        try:
            return self.dll.DAQ217_StartCollection(handle)
        except Exception:
            return False
    
    def stop_collection(self, handle: ctypes.POINTER(ctypes.c_uint32)) -> bool:
        """
        停止数据采集
        
        Args:
            handle: 设备句柄
            
        Returns:
            是否成功
        """
        try:
            return self.dll.DAQ217_StopCollection(handle)
        except Exception:
            return False
    
    def try_read_data(self, handle: ctypes.POINTER(ctypes.c_uint32), 
                     channel: int, read_size: int, timeout: int) -> Tuple[bool, List[float]]:
        """
        从指定通道读取数据（带超时）
        
        Args:
            handle: 设备句柄
            channel: 通道号
            read_size: 读取数据大小
            timeout: 超时时间（毫秒）
            
        Returns:
            (是否成功, 数据列表)
        """
        try:
            read_buffer = self._create_float_array(read_size)
            success = self.dll.DAQ217_TryReadData(handle, channel, read_buffer, read_size, timeout)
            if success:
                return True, self._convert_float_array_to_list(read_buffer)
            else:
                return False, []
        except Exception:
            return False, []
    
    def try_read_data_point(self, handle: ctypes.POINTER(ctypes.c_uint32), 
                           channel: int) -> Tuple[bool, float]:
        """
        从指定通道读取单个数据点
        
        Args:
            handle: 设备句柄
            channel: 通道号
            
        Returns:
            (是否成功, 数据值)
        """
        try:
            read_buffer = ctypes.c_float()
            success = self.dll.DAQ217_TryReadData_Point(handle, channel, ctypes.byref(read_buffer))
            if success:
                return True, read_buffer.value
            else:
                return False, 0.0
        except Exception:
            return False, 0.0
    
    def try_read_data_batch(self, handle: ctypes.POINTER(ctypes.c_uint32), 
                           channel: int, read_size: int) -> Tuple[bool, List[float]]:
        """
        从指定通道批量读取数据
        
        Args:
            handle: 设备句柄
            channel: 通道号
            read_size: 读取数据大小
            
        Returns:
            (是否成功, 数据列表)
        """
        try:
            read_buffer = self._create_float_array(read_size)
            success = self.dll.DAQ217_TryReadData_Batch(handle, channel, read_buffer, read_size)
            if success:
                return True, self._convert_float_array_to_list(read_buffer)
            else:
                return False, []
        except Exception:
            return False, []
    
    def clear_data_buffer(self, handle: ctypes.POINTER(ctypes.c_uint32)) -> bool:
        """
        清空所有数据缓冲区
        
        Args:
            handle: 设备句柄
            
        Returns:
            是否成功
        """
        try:
            return self.dll.DAQ217_ClearDataBuffer(handle)
        except Exception:
            return False
    
    def clear_data_buffer_channel(self, handle: ctypes.POINTER(ctypes.c_uint32), 
                                 channel: int) -> bool:
        """
        清空指定通道的数据缓冲区
        
        Args:
            handle: 设备句柄
            channel: 通道号
            
        Returns:
            是否成功
        """
        try:
            return self.dll.DAQ217_ClearDataBuffer_Channel(handle, channel)
        except Exception:
            return False
    
    def get_device_sn(self, handle: ctypes.POINTER(ctypes.c_uint32)) -> str:
        """
        获取设备序列号
        
        Args:
            handle: 设备句柄
            
        Returns:
            设备序列号
        """
        try:
            buffer = self._create_string_buffer(256)
            success = self.dll.DAQ217_GetSN(handle, buffer, 256)
            if success:
                return self._decode_string_buffer(buffer)
            else:
                return ""
        except Exception:
            return ""
    
    def get_device_name(self, handle: ctypes.POINTER(ctypes.c_uint32)) -> str:
        """
        获取设备名称
        
        Args:
            handle: 设备句柄
            
        Returns:
            设备名称
        """
        try:
            buffer = self._create_string_buffer(256)
            success = self.dll.DAQ217_GetDeviceName(handle, buffer, 256)
            if success:
                return self._decode_string_buffer(buffer)
            else:
                return ""
        except Exception:
            return ""
    
    def get_device_type(self, handle: ctypes.POINTER(ctypes.c_uint32)) -> str:
        """
        获取设备类型
        
        Args:
            handle: 设备句柄
            
        Returns:
            设备类型
        """
        try:
            buffer = self._create_string_buffer(256)
            success = self.dll.DAQ217_GetDeviceType(handle, buffer, 256)
            if success:
                return self._decode_string_buffer(buffer)
            else:
                return ""
        except Exception:
            return ""
    
    def get_hardware_version(self, handle: ctypes.POINTER(ctypes.c_uint32)) -> str:
        """
        获取硬件版本
        
        Args:
            handle: 设备句柄
            
        Returns:
            硬件版本
        """
        try:
            buffer = self._create_string_buffer(256)
            success = self.dll.DAQ217_GetHardwareVersion(handle, buffer, 256)
            if success:
                return self._decode_string_buffer(buffer)
            else:
                return ""
        except Exception:
            return ""
    
    def get_software_version(self, handle: ctypes.POINTER(ctypes.c_uint32)) -> str:
        """
        获取软件版本
        
        Args:
            handle: 设备句柄
            
        Returns:
            软件版本
        """
        try:
            buffer = self._create_string_buffer(256)
            success = self.dll.DAQ217_GetSoftwareVersion(handle, buffer, 256)
            if success:
                return self._decode_string_buffer(buffer)
            else:
                return ""
        except Exception:
            return ""
    
    def get_current_sample_rate(self, handle: ctypes.POINTER(ctypes.c_uint32)) -> int:
        """
        获取当前采样率
        
        Args:
            handle: 设备句柄
            
        Returns:
            当前采样率
        """
        try:
            return self.dll.DAQ217_GetCurrentSampleRate(handle)
        except Exception:
            return 0
    
    def get_adc_legal_channel_size(self, handle: ctypes.POINTER(ctypes.c_uint32)) -> int:
        """
        获取ADC合法通道数量
        
        Args:
            handle: 设备句柄
            
        Returns:
            通道数量
        """
        try:
            return self.dll.DAQ217_GetADCLegalChannelSize(handle)
        except Exception:
            return 0
    
    def get_adc_buffer_data_size(self, handle: ctypes.POINTER(ctypes.c_uint32), 
                                channel: int) -> int:
        """
        获取ADC缓冲区数据大小
        
        Args:
            handle: 设备句柄
            channel: 通道号
            
        Returns:
            缓冲区数据大小
        """
        try:
            return self.dll.DAQ217_GetADCBufferDataSize(handle, channel)
        except Exception:
            return 0
    
    def get_adc_buffer_data_max_size(self, handle: ctypes.POINTER(ctypes.c_uint32)) -> int:
        """
        获取ADC缓冲区最大大小
        
        Args:
            handle: 设备句柄
            
        Returns:
            缓冲区最大大小
        """
        try:
            return self.dll.DAQ217_GetADCBufferDataMaxSize(handle)
        except Exception:
            return 0
    
    def get_adc_legal_length_min(self, handle: ctypes.POINTER(ctypes.c_uint32)) -> int:
        """
        获取ADC合法最小长度
        
        Args:
            handle: 设备句柄
            
        Returns:
            最小长度
        """
        try:
            return self.dll.DAQ217_GetADCLegalLengthMin(handle)
        except Exception:
            return 0
    
    def get_adc_legal_length_max(self, handle: ctypes.POINTER(ctypes.c_uint32)) -> int:
        """
        获取ADC合法最大长度
        
        Args:
            handle: 设备句柄
            
        Returns:
            最大长度
        """
        try:
            return self.dll.DAQ217_GetADCLegalLengthMax(handle)
        except Exception:
            return 0
    
    def get_channel_enable_state(self, handle: ctypes.POINTER(ctypes.c_uint32)) -> int:
        """
        获取通道使能状态
        
        Args:
            handle: 设备句柄
            
        Returns:
            通道使能状态
        """
        try:
            return self.dll.DAQ217_GetChannelEnableState(handle)
        except Exception:
            return 0
    
    def adc_sample_rate_is_legal(self, handle: ctypes.POINTER(ctypes.c_uint32), 
                                sample_rate: int) -> bool:
        """
        验证采样率是否合法
        
        Args:
            handle: 设备句柄
            sample_rate: 采样率
            
        Returns:
            是否合法
        """
        try:
            return self.dll.DAQ217_ADCSampleRateIsLegal(handle, sample_rate)
        except Exception:
            return False
    
    def adc_range_is_legal(self, handle: ctypes.POINTER(ctypes.c_uint32), 
                          range_value: int) -> bool:
        """
        验证电压范围是否合法
        
        Args:
            handle: 设备句柄
            range_value: 电压范围值
            
        Returns:
            是否合法
        """
        try:
            return self.dll.DAQ217_ADCRangeIsLegal(handle, range_value)
        except Exception:
            return False
    